package com.million.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.cmsservice.entity.CrmBanner;
import com.million.cmsservice.mapper.CrmBannerMapper;
import com.million.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.servicebase.exception.GuliException;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-01
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    //将banner 图片放到缓存中
    //这个key 别忘了加 '' 这个符号
    //查询所有的banner
    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public List<CrmBanner> getAllBanner () {
        //根据id 排序降序排序，取前无条
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 5");
        List<CrmBanner> crmBanners = baseMapper.selectList(wrapper);
        if (crmBanners.size() == 0) {
            throw new GuliException(20001, "查询不到图片信息");
        }
        return crmBanners;
    }
}
